package io.github.biezhi.elves.selenium;

import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import io.github.biezhi.elves.common.Constants;
import io.github.biezhi.elves.model.CrawlWebsiteVerificationCode;
import io.github.biezhi.elves.utils.CreateElementScreenshot;
import io.github.biezhi.elves.utils.StringUtils;
import io.github.biezhi.elves.utils.WebDriverUtils;

/**
 * 爬取网站验证码
 *
 */
public class CrawlWebsiteSelenium {
	static WebDriver driver;
	
	/**
	 * 爬取网站验证码
	 * @param code
	 */
	public static void CrawlWebsiteImgVerificationCode(CrawlWebsiteVerificationCode code) {
		String url = code.getWebsiteUrl();
		System.out.println("class io.github.biezhi.elves.selenium.CrawlWebsiteSelenium，访问地址{{}}==>" + url);
		// 1、开启个浏览器并且输入链接
		try{
			boolean isKillChrome = false;
			//检测程序
			if(!StringUtils.findProcess(Constants.CHROME_PROCESS_NAME) && StringUtils.findAndKillProcess(Constants.CHROME_DRIVER_PROCESS_NAME)){
				//检测并杀掉程序
				StringUtils.findAndKillProcess(Constants.CHROME_DRIVER_PROCESS_NAME);
				isKillChrome = true;
			}
			
			if(!isKillChrome) {
				//如果没有发现chrome浏览器进程以及chromedriver驱动，则重置isKillChrome标识为true
				if(!StringUtils.findProcess(Constants.CHROME_PROCESS_NAME) && !StringUtils.findAndKillProcess(Constants.CHROME_DRIVER_PROCESS_NAME)){
					isKillChrome = true;
				}
			}
			
			if(driver==null || isKillChrome){
				WebDriver driver1 = WebDriverUtils.getChromeDriver(url);
				driver = driver1;
			}else{
				driver.get(url);
//				driver.navigate().refresh();
			}
			
			// 休息3秒，加载数据
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			List<WebElement> contentCodeElements = driver.findElements(By.xpath(code.getCodePrompt()));
			if(contentCodeElements!=null && contentCodeElements.size()>0){
				WebElement contentCodeElement = driver.findElement(By.xpath(code.getCodePrompt()));
				String contentCodeText = contentCodeElement.getText();
				contentCodeText = contentCodeText.replace("请点击图中的“", "").replace("”字", "");
				
				List<WebElement> imgElements = driver.findElements(By.cssSelector(code.getImgElement()));
				if(imgElements!=null && imgElements.size()>0){
					WebElement imgElement = driver.findElement(By.cssSelector(code.getImgElement()));
					try {
						FileUtils.copyFile(CreateElementScreenshot.captureElement(imgElement), new File(code.getFilePath()));
					} catch (Exception e) {
						System.out.println("保存验证码图片操作异常,异常信息如下{{}}==>" + StringUtils.getErrorInfoFromException(e));
					}
				}
			}
			
			//点击【提交】按钮
			List<WebElement> submitLoginElements = driver.findElements(By.xpath(code.getSubmitElement()));
			if(submitLoginElements!=null && submitLoginElements.size()>0){
				System.out.println("点击【提交】按钮......");
				WebElement submitLoginElement = driver.findElement(By.xpath(code.getSubmitElement()));
				WebDriverUtils.scrollToElementAndClick(submitLoginElement, driver);
			}
//			driver.quit();
		}catch(Exception e){
			//检测并杀掉程序
			StringUtils.findAndKillProcess(Constants.CHROME_DRIVER_PROCESS_NAME);
			
			System.out.println("io.github.biezhi.elves.selenium.QccSelenium类CrawlWebsiteSelenium方法，爬取网站验证码信息异常{{}}==>" + StringUtils.getErrorInfoFromException(e));
			System.out.println("休眠5000毫秒......");
			try{
				Thread.sleep(5000);
			}catch(Exception e1){}
			
			System.out.println("休眠5000毫秒后，再次调用爬取网站验证码方法");
			CrawlWebsiteImgVerificationCode(code);
		}
	}
	
	public static void main(String[] args) {
		CrawlWebsiteVerificationCode code = new CrawlWebsiteVerificationCode("http://my.job592.com/baike/myzb_search.action",
				"//*[@id=\"ct_js_dialog\"]/div[2]/table/tbody/tr[1]/td[1]", "iframe > .container > form > ul > li > #codeImg",
				"//*[@id=\"aform\"]/ul/li[4]/input[2]" ,"");
		CrawlWebsiteImgVerificationCode(code);
	}
}